package com.thfw.test.view.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.thfw.wmd.R;
import com.thfw.test.view.fragment.AboutMeFragment;
import com.thfw.test.view.fragment.DeviceManagementFragment;
import com.thfw.test.view.fragment.EnergyManagementFragment;
import com.thfw.test.view.fragment.LightControlFragment;


import butterknife.BindView;
import utils.ViewSelectGroup;

/**
 * Created BY Cuishikuntv_label
 * Created ON 2021/11/10
 * Package Name com.thfw.test.view.activity
 **/
public class MainActivity extends BaseActivity {

    private static final String TAG = MainActivity.class.getCanonicalName();

    @BindView(R.id.pager)
    ViewPager viewPager;
    @BindView(R.id.tabWidget)
    LinearLayout tabWidget;
    @BindView(R.id.tv_label)
    TextView tv_label;
    ViewSelectGroup tabGroup;
    private SectionsPagerAdapter sectionsPagerAdapter;
    LightControlFragment mLightControlFragment;
    EnergyManagementFragment energyManagementFragment;
    DeviceManagementFragment deviceManagementFragment;
    AboutMeFragment aboutMeFragment;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {


        tabGroup = new ViewSelectGroup();
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                Log.i(TAG, "onPageSelected(" + position + ")");
                tabGroup.setSelectedIndex(position);

            }
        });


        for (int i = 0; i < tabWidget.getChildCount(); i++) {
            tabGroup.addView(tabWidget.getChildAt(i));
        }


        // Set up the ViewPager with the sections adapter.
        sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(sectionsPagerAdapter);

        tabGroup.setListener(new ViewSelectGroup.OnSelectChangeListener() {
            @Override
            public void onViewSelectChanged(View v, int position) {
                Log.i(TAG, "onViewSelectChanged(" + position + ")");
                try {
                    viewPager.setCurrentItem(position, false);
                    switch (position){
                        case 0:
                            tv_label.setText("能耗管理");
                            break;
                        case 1:
                            tv_label.setText("安防管理");
                            break;
                        case 2:
                            tv_label.setText("灯控管理");
                            break;
                        case 3:
                            tv_label.setText("设备管理");
                            break;
                        case 4:
                            tv_label.setText("我的");
                            break;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        tabGroup.setSelectedIndex(2);
    }


    private class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (mLightControlFragment == null) {
                mLightControlFragment = LightControlFragment.newInstance();
                energyManagementFragment = EnergyManagementFragment.newInstance();
                deviceManagementFragment = DeviceManagementFragment.newInstance();
                aboutMeFragment = AboutMeFragment.newInstance();
            }

            switch (position) {
                case 0:
                    return energyManagementFragment;
                case 1:
                    return PlaceholderFragment.newInstance();
                case 2:
                    return mLightControlFragment;
                case 3:
                    return deviceManagementFragment;
                case 4:
                    return aboutMeFragment;
                default:
                    return PlaceholderFragment.newInstance();
            }
        }

        @Override
        public int getCount() {
            return getTabButtonCount();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return getTabButtonTitle(position);
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            super.setPrimaryItem(container, position, object);

            //
        }
    }

    protected int getTabButtonCount() {
        return tabWidget.getChildCount();
    }


    protected CharSequence getTabButtonTitle(int position) {
        if (position < tabWidget.getChildCount()) {
            View view = tabWidget.getChildAt(position);
            if (view instanceof TextView) {
                return ((TextView) view).getText();
            }
        }
        return "Button-" + position;
    }


    public static class PlaceholderFragment extends Fragment {

        public static PlaceholderFragment newInstance() {
            return new PlaceholderFragment();
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_not_impl, container, false);
        }
    }

}
